Skip to content

Stop the sending thread when the constructor fails after starting it - #17

Merged
dmccoystephenson merged 2 commits into
mainfrom
feature/stop-thread-when-init-throws
Oct 24, 2026
Merged

dmccoystephenson merged 2 commits into
mainfrom
feature/stop-thread-when-init-throws

Conversation

@dmccoystephenson

@dmccoystephenson dmccoystephenson commented Oct 24, 2026 •

Copy link
Copy Markdown
Member

Summary

  • TraceClient::init()'s catch (...) now calls close(0.0) instead of shared_.reset(). When anything throws after start() has launched the sending thread (the one known case is std::bad_alloc copying an explicit install ID into installId_), the thread is now stopped and joined or detached exactly as close() does it. Previously thread_ was left joinable, close() returned early because shared_ was empty, and ~TraceClient called std::terminate, which broke the README's "never throws" promise in its strongest form.
  • The test binary now replaces operator new with a thread-local countdown that fails the Nth allocation on the calling thread, once. The new test everyAllocationTheConstructorMakesMayFailWithoutAborting makes each allocation in the constructor fail in turn. Each failure must leave a disabled client (unavailable, no install ID) that can be destroyed safely. The loop ends at the first construction where no allocation was failed, and that client must be enabled and carry the ID. The sending thread and the StubServer threads are unaffected because the counter is thread-local.

Closes #16

Test plan

  • make test (g++, C++11): 55 tests, 0 failed checks (54 before, plus 1)
  • make test STD=c++17 and STD=c++2a (local g++ does not accept c++20): 55 tests, 0 failed checks. The sized operator delete is defined for C++14+ so that -Wsized-deallocation does not fail -Werror.
  • ASan+UBSan (g++): 55 tests, 0 failed checks
  • Regression check: with trace_client.hpp stashed, the suite aborts with terminate called without an active exception. With the fix restored, it passes.
  • CI is green on all 11 jobs: linux (g++/clang++ × c++11/17/20), linux-libcurl, linux-sanitizers (thread; address,undefined), macos, windows (MSVC).
  • Clang's ASan and TSan runtimes define operator new in a static archive, and the first CI run failed to link with multiple definition of operator new. The replacement is therefore compiled out under those sanitizers (SANITIZER_OWNS_NEW). In the two sanitizer jobs the new test constructs a client once and fails no allocation, so the fault injection runs in the other 9 jobs only. TSan could not be linked locally because libtsan_preinit.o is missing.

Notes

This PR description was drafted during a Gardener session (https://github.com/Stephenson-Software/gardener).

🤖 Generated with Claude Code


drafted by Claude on behalf of Daniel Stephenson

dmccoystephenson and others added 2 commits October 23, 2026 20:37
…16)

A throw in init() after start() reset shared_ but left thread_ joinable,
so close() found nothing to close and the destructor called
std::terminate. The catch now closes with no wait, which stops and
detaches (or joins) the thread exactly as close() does.

The test fails each allocation the constructor makes in turn, on the
calling thread only, through a counting operator new in the test binary.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Clang's sanitizer runtimes define operator new in a static archive, so a
replacement in the test binary fails to link there. Under them the
allocation-failure test constructs once without failing anything.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@dmccoystephenson

Copy link
Copy Markdown
Member Author

Self-review rubric (head 2688720):

  • Scope: PASS. Only trace_client.hpp (the init() catch) and test/test_trace_client.cpp changed, and both are needed for A throw in init() after start() leaves thread_ joinable, so the destructor calls std::terminate #16.
  • Tests-new: PASS. No public API was added. The behavior is covered by the new everyAllocationTheConstructorMakesMayFailWithoutAborting.
  • Tests-fix: PASS. With trace_client.hpp stashed, make test aborted with terminate called without an active exception. With the fix restored, it printed 55 tests, 0 failed checks.
  • Sibling structure: PASS. No new files were added. The test follows the existing static void sentence-name and CHECK/CHECK_EQ conventions.
  • Sibling renames: PASS. Nothing was renamed.
  • Docs: PASS. The README already promises that the client never throws, and this change makes that promise hold. No README, version, wire-format or build text describes this path.
  • Issue resolution: PASS. A throw in init() after start() leaves thread_ joinable, so the destructor calls std::terminate #16 names the catch (...) in init() that leaves thread_ joinable, and that catch now calls close(0.0).
  • CI: PASS. All 11 jobs are green on run 40055105721.
  • Registered: PASS. The new name appears twice in the file (definition and tests[]), and the count rose from 54 to 55.
  • C++11: PASS. make test (C++11, -Werror) is clean locally, and both linux c++11 legs are green. The sized operator delete is guarded by __cplusplus >= 201402L so that -Wsized-deallocation passes on the c++17/20 legs.
  • No new dependency: PASS. The header gains no #include, and Makefile/CMakeLists.txt are unchanged. <new> is added to the test only.
  • Never throws: PASS. close() catches everything internally, and installId_.clear() and the reason_ assignment were already in this catch.
  • Curl argv fixed: PASS. The diff does not touch the spawn, argv or config quoting.
  • Version triple: PASS. The version is unchanged.
  • Sanitizers: PASS with a caveat. Both jobs are green, but the replacement operator new is compiled out under ASan/TSan because Clang's runtimes define it. In those two jobs the new test does no fault injection, and it runs in the other 9 jobs.

Observations:

  • test/test_trace_client.cpp:64: the replacement operator new applies to the whole test binary. It fails an allocation only while the calling thread's counter is armed, and the counter is armed only inside the new test.
  • trace_client.hpp:1107: close(0.0) normally takes the "abandoned + detach" branch because the zero deadline has already passed. The detached thread holds its own shared_ptr and sees closing with an empty queue, so it exits on its own. This is the same path closeReturnsWithinTheTimeoutWhenTheServerHangs already exercises under TSan.

This self-review was drafted during a Gardener session (https://github.com/Stephenson-Software/gardener).


drafted by Claude on behalf of Daniel Stephenson

@dmccoystephenson
dmccoystephenson merged commit 9f1154c into main Oct 24, 2026
11 checks passed
@dmccoystephenson
dmccoystephenson deleted the feature/stop-thread-when-init-throws branch October 24, 2026 02:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A throw in init() after start() leaves thread_ joinable, so the destructor calls std::terminate

1 participant